<template>
  <div id="container">
    <div class="overview">
      <h2 class="title">今日总览</h2>
      <div class="cards-wrap">
        <el-card shadow="hover">
          <el-statistic
            group-separator=","
            :value="parseInt(todayData.postCount)"
            :value-style="valueStyle"
            ><span slot="title" style="color: #606266">发帖</span></el-statistic
          >
          <i class="el-icon-s-promotion"></i>
        </el-card>
        <el-card shadow="hover">
          <el-statistic
            group-separator=","
            :value="parseInt(todayData.followCount)"
            :value-style="valueStyle"
            title="关注"
            ><span slot="title" style="color: #606266">关注</span></el-statistic
          >
          <i class="el-icon-plus"></i>
        </el-card>
        <el-card shadow="hover">
          <el-statistic
            group-separator=","
            :value="parseInt(todayData.viewCount)"
            :value-style="valueStyle"
            title="访问"
            ><span slot="title" style="color: #606266">访问</span></el-statistic
          >
          <i class="el-icon-user"></i>
        </el-card>
        <el-card shadow="hover">
          <el-statistic
            group-separator=","
            :value="parseInt(todayData.commentCount)"
            :value-style="valueStyle"
            title="评论"
            ><span slot="title" style="color: #606266">评论</span></el-statistic
          >
          <i class="el-icon-s-comment"></i>
        </el-card>
        <el-card shadow="hover">
          <el-statistic
            group-separator=","
            :value="parseInt(todayData.likeCount)"
            :value-style="valueStyle"
            title="点赞"
            ><span slot="title" style="color: #606266">点赞</span></el-statistic
          >
          <i class="fa fa-thumbs-o-up" aria-hidden="true"></i>
        </el-card>
      </div>
    </div>
    <div chart-wrap>
      <h2 class="title">7日数据</h2>
      <el-select
        v-model="selectValue"
        @change="handleselectChange"
        default-first-option
      >
        <el-option
          v-for="item in selectOps"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <div class="chart-view" ref="chart"></div>
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts/core";
import { GridComponent } from "echarts/components";
import { LineChart } from "echarts/charts";
import { UniversalTransition } from "echarts/features";
import { CanvasRenderer } from "echarts/renderers";
import forumApi from "@/axios/forumApi";
import * as date from "date-fns";

echarts.use([GridComponent, LineChart, CanvasRenderer, UniversalTransition]);
export default {
  props: ["forumId"],
  data() {
    return {
      selectValue: "postCountList",
      myChart: {},
      valueStyle: {
        fontSize: "32px",
      },
      todayData: {},
      weekData: {},
      options: {
        xAxis: {
          type: "category",
          data: this.getSevenDaysArray(),
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: [],
            type: "line",
            label: {
              show: true,
              position: "top",
              textStyle: {
                fontSize: 20,
              },
            },
            smooth: true,
          },
        ],
      },
      selectOps: [
        {
          value: "postCountList",
          label: "发帖数",
        },
        {
          value: "commentCountList",
          label: "评论数",
        },
        {
          value: "likeCountList",
          label: "点赞数",
        },
        {
          value: "viewCountList",
          label: "访问数",
        },
        {
          value: "followCountList",
          label: "关注数",
        },
      ],
    };
  },
  mounted() {},
  methods: {
    handleselectChange(v) {
      console.log("change");
      this.options.series.data = this.weekData[v];
      this.options.series[0].data = this.weekData[this.selectValue];
      this.myChart.setOption(this.options);
    },
    getSevenDaysArray() {
      let dateArr = [];
      console.log(date);
      let day = Date.now();
      dateArr.push(date.format(day, "MM-dd"));
      for (let i = 0; i < 6; i++) {
        day = date.subDays(day, 1);
        dateArr.unshift(date.format(day, "MM-dd"));
      }
      return dateArr;
    },
  },
  created() {
    forumApi.getTodayStatistics(this.forumId).then(({ data }) => {
      this.todayData = data.data;
    });
    forumApi.getWeekStatistics(this.forumId).then(({ data }) => {
      this.weekData = Object.freeze(data.data);

      this.options.series[0].data = this.weekData[this.selectValue];
      this.myChart = echarts.init(this.$refs.chart);
      this.myChart.setOption(this.options);
    });
  },
};
</script>

<style scoped lang="less">
#container {
  padding: 30px;
}

h2.title {
    font-weight: 400;
    margin-bottom: 30px;
    color: var(--text-regular);
  }
.cards-wrap {
  min-width: 800px;
  height: 200px;
  display: flex;
  justify-content: space-around;
  .el-card {
    width: 150px;
    height: 150px;
    text-align: center;
    // display: flex;
    // flex-direction: column;
    i {
      margin-top: 10px;
      font-size: 32px;
      color: var(--text-primary);
    }
  }
}
.chart-view {
  width: 100%;
  height: 500px;
}
</style>